import { Component} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

// 引入Parse第三方库
import * as Parse from "parse"
(Parse as any).serverURL = "http://metapunk.cn:9999/parse"
Parse.initialize("dev")

import { CartService } from '../cart.service';
import { query } from '@angular/animations';

@Component({
  selector: 'app-page-detail',
  templateUrl: './page-detail.component.html',
  styleUrls: ['./page-detail.component.scss']
})
export class PageDetailComponent {
  public alertButtons = ['OK'];
  item:Parse.Object|null = null
  constructor(
        private route:ActivatedRoute,
        private router:Router,
        private cartServ:CartService
        ){
       /**
     * Angular的ActivatedRoute服务，实现参数解析
     */
    this.route.queryParams.subscribe(async params=>{
        console.log(params)
        let id = params['id']
        if(id){
           this.item = await this.getDetailById(id)
        }
      })
  
    }

  async getDetailById(id:string){
    let query = new Parse.Query("goods")
    let goods = await query.get(id)
    console.log(goods)

    this.getCommentBygoodsID(goods.get("goodsID"))
    return goods
  }
  CommentList:any

  async getCommentBygoodsID(goodsID:string){
    let query = new Parse.Query("comment")
    query.equalTo("goodsID",goodsID)
    let list  = await query.find()
    console.log(list)
    this.CommentList = list
    return list
  }

    goBack(){
      this.router.navigate(["./intelligent-shopping/home"])
    }
  
    addCart(){
      this.cartServ.addGoodsToCart(this.item,1)
      // this.router.navigate(["./intelligent-shopping/shoppingcart"])
   }


   async updateGoodsNumber(goodsID:string){
    const query = new Parse.Query("goods")
    const goods = await query.get(goodsID)
    if(goods){
      const currentNumber = goods.get("goodsNumber");
      goods.set("goodsNumber",currentNumber+1)
      await goods.save()
    }
  }
  
  
   async goPay() {
   console.log(this.item?.get('goodsID')+'goodsid',this.item?.get('goodsNumber')+'goodsnumber')
   const goodsID = this.item?.get("goodsID")
   if(goodsID){
    // await this.updateGoodsNumber(goodsID)
   }
    this.cartServ.addGoodsToCart(this.item,1)

    // 修改goodsNumber
//     let query2 = new Parse.Query("goods")
//     let stu1 = await query2.get("sVJaK06YzB");
//     stu1.set("goodsID","006")
//     stu1.save()
    console.log("(this.cartServ.CartList",this.cartServ.CartList)
      this.router.navigate(["./intelligent-shopping/pay"],{
          queryParams:{id:this.item?.id}
      })
    }

    //查看商品评价
    goComment(comment:any){
      console.log(comment+"111")
   
        console.log(comment.id);
         this.router.navigate(["./intelligent-shopping/comment"],{
            queryParams:{
                id:comment.id
      }
         })
      }
    
   }

